package org.syncsoft.overalt.gui.impl.window.shareFolder;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.syncsoft.overalt.common.exception.SystemException;
import org.syncsoft.overalt.core.api.Core;
import org.syncsoft.overalt.core.api.ICore;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.folder.entity.TypeShare;
import org.syncsoft.overalt.domain.tag.entity.Tag;
import org.syncsoft.overalt.domain.user.entity.User;
import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.window.AbstractWindowController;

/**
 * 
 * @author tptfc
 *
 */
public class ShareFolderWindowControll extends AbstractWindowController<ShareFolderWindow> {
	private String path;
	
	public ShareFolderWindowControll (String path) {
		setPath(path);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doCreateWindow()
	 */
	@Override
	protected ShareFolderWindow doCreateWindow() throws SystemException {
		ShareFolderWindow window = new ShareFolderWindow(this);
		
		Collection<Community> communities = getCore().getComunities();
		Collection<User> contacts = getCore().getContacts();
		
		window.setCommunities(communities);
		window.setContacts(contacts);
		
		return window;
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doSubmit(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	protected void doSubmit(WindowContextValue context) throws SystemException {
		TypeShare typeShare = context.getValue("typeShare");
		boolean autoTitleAndDescription = context.getValue("autoTitleAndDescription") != null;
		Collection<Integer> contactsCode = context.getValues("contacts");
		Community community = context.getValue("community");
		Integer communityCode = null;
		if (community != null) {
			communityCode = community.getCode();
		}
		Collection<Integer> membersCode = context.getValues("members");
		boolean autoSync = context.getValue("autoSync") != null;
		
		String tagsString = context.getValue("tags");
		Set<Tag> tags = parseTags(tagsString);
		
		Core.getInstance().shareFolder(getPath(), typeShare, autoTitleAndDescription, new HashSet<Integer>(contactsCode), communityCode, new HashSet<Integer>(membersCode), autoSync, null, tags);
	}
	
	public static void main(String[] args) {
		ShareFolderWindowControll controll = new ShareFolderWindowControll("/home/tptfc");
		controll.start();
	}
	
	protected ICore getCore() {
		return Core.getInstance();
	}

	private String getPath() {
		return path;
	}
	private void setPath(String path) {
		this.path = path;
	}
}
